import json
import re

# 读取符号表并构建地址到名称的映射
with open('./caches_data/symbols_export.json', 'r', encoding='utf-8') as f:
    symbols_data = json.load(f)
symbol_dict = {
    int(symbol['address']): symbol['name']
    for symbol in symbols_data['symbols']
}

# 定义地址匹配的正则表达式（支持十六进制/十进制）
address_pattern = re.compile(r'\b(0x[0-9a-fA-F]+|\d+)\b')

# 处理文件
with open('./caches_data/nokia_path/common.txt', 'r', encoding='utf-8') as infile, \
     open('./caches_data/output.txt', 'w', encoding='utf-8') as outfile:
    
    for line in infile:
        # 查找所有地址匹配项
        matches = address_pattern.finditer(line)
        parts = []
        last_end = 0
        
        for match in matches:
            # 提取原始地址文本
            original = match.group(0)
            # 转换地址格式
            if original.startswith('0x'):
                num = int(original, 16)
            else:
                num = int(original)
            
            # 获取符号名称（保留未知地址）
            name = symbol_dict.get(num, 'UNKNOWN_SYMBOL')
            parts.append(line[last_end:match.start()])
            parts.append(str(name))
            last_end = match.end()
        
        # 添加剩余部分
        parts.append(line[last_end:])
        
        # 重组完整行
        new_line = ''.join(parts)
        outfile.write(new_line)